package com.example.client;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import util.PermissionUtil;

public class PermissionLazyActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_readpep;
    private Button btn_sendrecv;
    private static final String[] PERMISSION_CONTACTS = new String[]{
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS
    };
    private static final String[] PERMISSION_SMS = new String[]{
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_SMS
    };
    private static final int REQUEST_CODE_CONTACTS = 1;
    private static final int REQUEST_CODE_SMS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_permission_lazy);

        btn_readpep = findViewById(R.id.btn_readpep);
        btn_sendrecv = findViewById(R.id.btn_sendrecvie);

        btn_sendrecv.setOnClickListener(this);
        btn_readpep.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_readpep){
            PermissionUtil.checkPermission(this,PERMISSION_CONTACTS,REQUEST_CODE_CONTACTS);
        }else if (v.getId() == R.id.btn_sendrecvie){
            PermissionUtil.checkPermission(this,PERMISSION_SMS,REQUEST_CODE_SMS);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_CODE_CONTACTS:
                if (PermissionUtil.checkGrant(grantResults)){
                    Log.e("ning", "读取通讯录权限申请成功 ");
                }else
                {
                    Toast.makeText(this,"读取通讯录授权失败",Toast.LENGTH_LONG);
                    jumpSettings();
                }
                break;
            case REQUEST_CODE_SMS:
                if (PermissionUtil.checkGrant(grantResults)){
                    Log.e("ning", "短信权限申请成功 ");
                }else
                {
                    Toast.makeText(this,"获取短信授权失败",Toast.LENGTH_LONG);
                    jumpSettings();
                }
                break;
        }
    }
    private void jumpSettings(){
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package",getPackageName(),null));
        startActivity(intent);
    }


}